// pages/dishespay/dishespay.js

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
const {
  showLoading,
  hideLoading
} = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    isScheduled: '',
    scheduledTime: '',
    orderType: '',
    restaurantId: '',
    totalPrice: '',
    orderNo: '',
    couponId: '',
    remark: '',
    couponPrice: 0,
    totalPricecoupon: '',
    carArray: [],
    packingCharges: 0,
    totalNum: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('carArray')) {
      this.data.carArray = wx.getStorageSync('carArray');
    }
    if (options.orderType) { //就餐类型 1食堂就餐去掉包装费 2打包就餐含包装费 
      this.data.orderType = options.orderType;
      if (options.orderType == '2') {
        var carArray = this.data.carArray;
        var totalNum = 0;
        for (var i = 0; i < carArray.length; i++) {
          totalNum += carArray[i].num;
        }
        this.setData({ //总价
          totalNum: totalNum, //包装费
          totalPricecoupon: parseInt(totalNum) + parseInt(options.totalPrice)
        })
      } else {
        this.setData({
          totalNum: 0, //包装费
          totalPricecoupon: options.totalPrice,
        })
      }
    }
    if (options.restaurantId) {
      this.data.restaurantId = options.restaurantId;
    }
    if (options.scheduledTime) {
      this.data.scheduledTime = options.scheduledTime;
    }
    if (options.isScheduled) {
      this.data.isScheduled = options.isScheduled;
    }
    if (options.orderNo) {
      this.data.orderNo = options.orderNo;
    }
    if (options.totalPrice) {
      this.data.totalPrice = options.totalPrice;
    }
    this.setData(this.data)
  },
  pay() {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.empty();
    prevPage.setData({
      cartShow: 'none',
    });
    let that = this;
    var openId = app.globalData.openId || wx.getStorageSync("OPENID");
    var id = app.globalData.id || wx.getStorageSync("id");
    if (that.data.totalPricecoupon <= 0) {
      wx.showToast({
        title: '你没有购买任何产品,先去加入购物车吧',
        icon: 'none'
      })
      return false
    }
    //清除点餐页面购物车

    var TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
    wx.request({
      url: applet_park_api_url + '/api/wechatPay',
      method: 'POST',
      header: {
        Authorization: TOKEN
      },
      data: {
        orderNo: that.data.orderNo,
        totalAmount: that.data.totalPricecoupon + '00', //that.data.totalPricecoupon+'00'
        sysUserId: id,
        openid: openId,
        remark: that.data.remark,
        couponId: that.data.couponId,
        paySource: 1
      },
      success(res2) {
        if (res2.data.code == 0) {
          console.log(res2.data.data)
          wx.requestPayment({
            timeStamp: res2.data.data.timeStamp,
            nonceStr: res2.data.data.nonceStr,
            package: res2.data.data.prepay_id,
            signType: 'MD5',
            paySign: res2.data.data.sign,
            paySource: 1,
            success(res) {
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function () {
                wx.showToast({
                  title: '页面跳转中',
                })

                wx.navigateTo({
                  url: '../orderDetails_canteen/orderDetails_canteen?orderNo=' + that.data.orderNo,
                })
              }, 1000);
            },
            fail(res3) {
              console.log(res3)
            }
          })

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })

  },
  getwechatPayResult() { //微信支付回调
    wx.request({
      url: applet_park_api_url + '/api/wechatPay',
      method: 'GET',
      header: {},
      data: {},
      success(res2) {

        if (res2.data.code == 0) {

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  gocoupon() {
    var couponType = 1;
    var that = this;
    wx.navigateTo({
      url: "../coupon_canteen/coupon_canteen?couponType=" + couponType + "&restaurantId=" + that.data.restaurantId + "&paypage=true" + "&totalPrice=" + this.data.totalPrice,

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})